package com.example.controller;

import com.example.entity.SecKillSku;
import com.example.entity.SecKillSpu;
import com.example.entity.SecKillUser;
import com.example.service.UserService;
import com.example.service.SkuService;
import com.example.service.SpuService;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.thymeleaf.util.StringUtils;

/**
 * (SecKillUser)表控制层
 *
 * @author 游家纨绔
 * @since 2023-09-05 22:00:00
 */
@Slf4j
@Controller
public class LoginController {

	/**
	 * 服务对象
	 */
	@Resource
	private UserService userService;
	@Autowired
	private SpuService spuService;
	@Autowired
	private SkuService skuService;

	@PostMapping("/login")
	public String login(String username, String password, Model model) {
		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			return "index";
		}
		SecKillUser secKillUser = userService.selectByUserName(username);
		if (Objects.isNull(secKillUser)) {
			return "index";
		}
		// 商品id
		SecKillSpu secKillSpu = spuService.selectOne(1);
		SecKillSku secKillSku = skuService.queryMinPriceSku(1);
		List<String> color = skuService.selectColor(1);
		model.addAttribute("spu", secKillSpu);
		model.addAttribute("sku", secKillSku);
		model.addAttribute("color", color);
		model.addAttribute("user", secKillUser);

		return "goods";
	}

}
